package com.weme.matching.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.makeramen.roundedimageview.RoundedImageView
import com.weme.common.util.loadImage
import com.weme.matching.R

class MyPagerAdapter : RecyclerView.Adapter<MyPagerAdapter.ViewHolder>() {

    private var currentPosition = 0

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_matching, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(position)
    }

    override fun getItemCount(): Int {
        // 为了实现无限循环，返回一个很大的值
        return Int.MAX_VALUE
    }

    fun setCurrentPosition(position: Int) {
        currentPosition = position
        notifyDataSetChanged()
    }
    var data = mutableListOf<String>()

    fun submitList(data : List<String>){
        this.data = data.toMutableList()
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val roundImg: RoundedImageView = itemView.findViewById(R.id.roundImg)

        fun bind(position: Int) {
            val realPosition = position % data.size // 获取真实的位置
            roundImg.loadImage(data[realPosition])
        }
    }
}
